﻿using _1._2._2015FullCrud.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace _1._2._2015FullCrud
{
    public class TodoService
    {

        #region private class static vars
        // sets our TodoService to our private instance
        private static TodoService _instance = null;
        public static TodoService Instance
        {
            // checks to see if we have a version of the private instance started if we do it just returns it if not it starts it then returns it
            get
            {
                if (_instance == null)
                {
                    _instance = new TodoService();
                }
                return _instance;
            }
        }

        #endregion

        #region private class instance Variables
        // starts a private version of the todo list
        private List<TodoViewModel> _todos;
        //sets a var for our id for each todo in the list
        private int _seedId;
        #endregion


        #region constructors
        private TodoService()
        {
            //sets our Id to 1 and starts our todo list
            _seedId = 1;
            _todos = new List<TodoViewModel>();

        }



        #endregion


        #region Class Static Methods
        //public static string GetTodoVersion()
        //{
            
        //    return "1.0";
        //}
        //public static TodoService GetTodoServiceInstance()
        //{
        //    return new TodoService();
        //}
        #endregion

        public List<TodoViewModel> GetTodos()
        {
            // gets the todos all of them
            return _todos;
        }
        public TodoViewModel GetTodo(int id)
        {
            //gets just the one todo from the list that we sent the id in for
            return _todos.FirstOrDefault(t => t.Id == id);
        }

        public int CreateTodo(string text)
        {
            //calls the model page and starts a new TodoViewModel to create a new todo to add to the list
            TodoViewModel model = new TodoViewModel
            {
                DateCreated = DateTime.Now,
                Text = text,
                Id = _seedId
            };
            //we add the todo we made above to the list we already have sitting and waiting
            _todos.Add(model);
            //adds 1 to the id so the next time it is called we get the next number in the list
            _seedId++;
            //returns the id
            return model.Id;
        }
        
        public void UpdateTodo(int id, string text, bool isCompleted)
        {
            //pulls the todo we need to make the changes to it needed
            TodoViewModel model = _todos.FirstOrDefault(t =>t.Id == id);
            //make sure its not null so data can be updated
            if (model == null)
                return;

            //if the if is good to go we set the info and move on
            model.Text = text;
            model.IsCompleted = isCompleted;
            //if it is marked completed update the time it was marked done
            if(isCompleted)
            {
                model.DateCompleted = DateTime.Now;
            }
        }
    }
    
}






